package com.iwinding.ladygg.ui.act;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.iwinding.ladygg.R;
import com.iwinding.ladygg.net.RequestManager;
import com.iwinding.ladygg.net.callback.BaseRpCallback;
import com.iwinding.ladygg.net.request.ChangePasswordReq;
import com.iwinding.ladygg.net.response.BaseRopnse;
import com.iwinding.ladygg.utils.ToastUtil;

/**
 * Created by hezhiyong on 2017/4/20.
 */

public class ChangePwActivity extends BaseActivity implements View.OnClickListener {

    AutoCompleteTextView mCompleteOldPw;
    AutoCompleteTextView mCompleteNewPw;
    AutoCompleteTextView mCompleteConfirmPw;

    ImageView mImg_clearOld;
    ImageView mImg_clearnew;
    ImageView mImg_clearConfirm;

    private ImageView mImgSecure_old;
    private ImageView mImgVisable_old;
    private ImageView mImgSecure_new;
    private ImageView mImgVisable_new;
    private ImageView mImgSecure_confirm;
    private ImageView mImgVisable_confirm;

    Button mBtn_confirm;

    @Override
    protected int getLayoutID() {
        return R.layout.icane_activity_change_pw;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        setLinster();
    }

    void initView() {
        setTitleResId(R.string.icane_my_modify_password);

        mCompleteOldPw = (AutoCompleteTextView) findViewById(R.id.icane_edit_old_pw);
        mCompleteNewPw = (AutoCompleteTextView) findViewById(R.id.icane_edit_new_pw);
        mCompleteConfirmPw = (AutoCompleteTextView) findViewById(R.id.icane_edit_pw_confirm);
        mImg_clearOld = (ImageView) findViewById(R.id.clear_old_pw);
        mImg_clearnew = (ImageView) findViewById(R.id.clear_new_pw);
        mImg_clearConfirm = (ImageView) findViewById(R.id.clear_pw_confirm);
        mBtn_confirm = (Button) findViewById(R.id.icane_confirm);

        mImgSecure_old = (ImageView) findViewById(R.id.icane_secure_old);
        mImgVisable_old = (ImageView) findViewById(R.id.icane_visable_old);
        mImgSecure_new = (ImageView) findViewById(R.id.icane_secure_new);
        mImgVisable_new = (ImageView) findViewById(R.id.icane_visable_new);
        mImgSecure_confirm = (ImageView) findViewById(R.id.icane_secure_confirm);
        mImgVisable_confirm = (ImageView) findViewById(R.id.icane_visable_confirm);

        mImgSecure_old.setVisibility(View.GONE);
        mImgVisable_old.setVisibility(View.GONE);
        mImgSecure_new.setVisibility(View.GONE);
        mImgVisable_new.setVisibility(View.GONE);
        mImgSecure_confirm.setVisibility(View.GONE);
        mImgVisable_confirm.setVisibility(View.GONE);

    }

    void setLinster() {
        mCompleteOldPw.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mImg_clearOld.setVisibility(s.length() == 0 ? View.GONE : View.VISIBLE);
            }
        });
        mCompleteNewPw.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mImg_clearnew.setVisibility(s.length() == 0 ? View.GONE : View.VISIBLE);
            }
        });
        mCompleteConfirmPw.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mImg_clearConfirm.setVisibility(s.length() == 0 ? View.GONE : View.VISIBLE);
            }
        });
        mBtn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                confirm();
            }
        });

        mImgSecure_old.setOnClickListener(this);
        mImgSecure_new.setOnClickListener(this);
        mImgSecure_confirm.setOnClickListener(this);

        mImgVisable_old.setOnClickListener(this);
        mImgVisable_new.setOnClickListener(this);
        mImgVisable_confirm.setOnClickListener(this);

        mImg_clearOld.setOnClickListener(this);
        mImg_clearnew.setOnClickListener(this);
        mImg_clearConfirm.setOnClickListener(this);
    }

    //确认
    void confirm() {
        String oldPw = mCompleteOldPw.getEditableText().toString().trim();
        String newPw = mCompleteNewPw.getEditableText().toString().trim();
        String confirmPw = mCompleteConfirmPw.getEditableText().toString().trim();
        if (TextUtils.isEmpty(oldPw)) {
            ToastUtil.openBaseToast(mContext, R.string.old_password_connot_be_empty, Toast.LENGTH_SHORT);
            return;
        }
        if (TextUtils.isEmpty(newPw)) {
            ToastUtil.openBaseToast(mContext, R.string.new_password_connot_be_empty, Toast.LENGTH_SHORT);
            return;
        }
        if (TextUtils.isEmpty(confirmPw)) {
            ToastUtil.openBaseToast(mContext, R.string.confirm_password_connot_be_empty, Toast.LENGTH_SHORT);
            return;
        }
        ChangePasswordReq changePasswordReq = new ChangePasswordReq();
        changePasswordReq.oldPwd = oldPw;
        changePasswordReq.newPwd = newPw;
        changePasswordReq.confirmPwd = confirmPw;

        RequestManager.postChangePassword(mContext, changePasswordReq, new BaseRpCallback<BaseRopnse>() {
            @Override
            public BaseRopnse parseResponse(BaseRopnse response, int id) {
                return response;
            }

            @Override
            public void onResult(BaseRopnse o) {
                ToastUtil.openBaseToast(mContext, R.string.icane_change_ok, Toast.LENGTH_SHORT);
                setResult(RESULT_OK);
                finish();
            }

            @Override
            public void onFail(String errorMsg) {
                ToastUtil.openBaseToast(mContext, errorMsg, Toast.LENGTH_SHORT);
            }

        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.icane_secure_old:
                mImgSecure_old.setVisibility(View.GONE);
                mImgVisable_old.setVisibility(View.VISIBLE);
                mCompleteOldPw.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                mCompleteOldPw.setSelection(mCompleteOldPw.getText().length());
                break;
            case R.id.icane_visable_old:
                mImgVisable_old.setVisibility(View.GONE);
                mImgSecure_old.setVisibility(View.VISIBLE);
                mCompleteOldPw.setTransformationMethod(PasswordTransformationMethod.getInstance());
                mCompleteOldPw.setSelection(mCompleteOldPw.getText().length());
                break;
            case R.id.icane_secure_new:
                mImgSecure_new.setVisibility(View.GONE);
                mImgVisable_new.setVisibility(View.VISIBLE);
                mCompleteNewPw.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                mCompleteNewPw.setSelection(mCompleteNewPw.getText().length());
                break;
            case R.id.icane_visable_new:
                mImgVisable_new.setVisibility(View.GONE);
                mImgSecure_new.setVisibility(View.VISIBLE);
                mCompleteNewPw.setTransformationMethod(PasswordTransformationMethod.getInstance());
                mCompleteNewPw.setSelection(mCompleteNewPw.getText().length());
                break;
            case R.id.icane_secure_confirm:
                mImgSecure_new.setVisibility(View.GONE);
                mImgVisable_new.setVisibility(View.VISIBLE);
                mCompleteConfirmPw.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                mCompleteConfirmPw.setSelection(mCompleteConfirmPw.getText().length());
                break;
            case R.id.icane_visable_confirm:
                mImgVisable_new.setVisibility(View.GONE);
                mImgSecure_new.setVisibility(View.VISIBLE);
                mCompleteConfirmPw.setTransformationMethod(PasswordTransformationMethod.getInstance());
                mCompleteConfirmPw.setSelection(mCompleteConfirmPw.getText().length());
                break;
            case R.id.clear_old_pw:
                mCompleteOldPw.setText("");
                break;
            case R.id.clear_new_pw:
                mCompleteNewPw.setText("");
                break;
            case R.id.clear_pw_confirm:
                mCompleteConfirmPw.setText("");
                break;

        }
    }
}
